
import {
  downloadFile
} from '@/api/fd/document'
export function fileDownload(row) {
  console.log(row)
  downloadFile({
    documentId: row.documentId
  }).then((res) => {
    const { url, documentName } = res.data
    getBlob(url).then((blob) => {
      saveAs(blob, documentName)
    })
  })
}
// 下载重命名
function getBlob(url) {
  return new Promise((resolve) => {
    const xhr = new XMLHttpRequest()
    xhr.open('GET', url, true)
    xhr.responseType = 'blob'
    xhr.onload = () => {
      if (xhr.status === 200) {
        resolve(xhr.response)
      }
    }

    xhr.send()
  })
}
// 下载重命名
function saveAs(blob, filename) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, filename)
  } else {
    const link = document.createElement('a')
    const body = document.querySelector('body')

    link.href = window.URL.createObjectURL(blob)
    link.download = filename
    link.style.display = 'none'
    body.appendChild(link)
    link.click()
    body.removeChild(link)
    window.URL.revokeObjectURL(link.href)
    this.a = this.a + 1
    console.log(this.a)
  }
}
